/* Copyright 2009 Requirements Management System 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.mags.remas.view.dialogs;

import java.util.List; 

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.mags.common.EngineContext;
import org.mags.remas.control.commons.IAction;
import org.mags.remas.model.entity.CommentType;
import org.mags.remas.view.commons.ImageProvider;

/**
 * 
 * @author Mario Gaitan
 *
 */
public class EditablePopupDialog extends org.eclipse.jface.dialogs.PopupDialog {
	private Text text;
	private IAction acceptAction;
	private IAction cancelAction;
	private String comment;
	private Combo comboTypes;
	private List<CommentType> types=EngineContext.getDataContext().getEntityProviderFactory().getCommentTypeProvider().getAll();
	
	public CommentType getType() {
		return types.get(comboTypes.getSelectionIndex());
	}
	public String getComment() {
		return comment;
	}
	public IAction getAcceptAction() {
		return acceptAction;
	}
	public void setAcceptAction(IAction acceptAction) {
		this.acceptAction = acceptAction;
	}
	public IAction getCancelAction() {
		return cancelAction;
	}
	public void setCancelAction(IAction cancelAction) {
		this.cancelAction = cancelAction;
	}
	protected Control createContents(Composite parent) {
		Composite composite = (Composite)super.createContents(parent);
		GridData layoutData = new GridData();
		layoutData.horizontalAlignment = SWT.RIGHT;
		Canvas buttons = new Canvas(composite, SWT.NONE);
		buttons.setBackground(super.getBackground());
		buttons.setLayoutData(layoutData);
		buttons.setLayout(new GridLayout(2, true));

		layoutData = new GridData();
		layoutData.horizontalAlignment = SWT.FILL;
		comboTypes = new Combo(composite, SWT.READ_ONLY);
		comboTypes.setLayoutData(layoutData);
		for(CommentType i: types)
			comboTypes.add(i.getDescription());
		comboTypes.select(types.indexOf(EngineContext.getDataContext().getEntityProviderFactory().getCommentTypeProvider().getOTHER()));
		
		Button btnAccept = new Button(buttons, SWT.NONE);
		btnAccept.setImage(ImageProvider.getCommonImage("save.png"));
		btnAccept.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
			@Override
			public void widgetSelected(SelectionEvent e) {
				comment = text.getText();
				if(acceptAction==null)
					close();
				else if(acceptAction.execute(null))
					close();				
			}
		});
		Button btnCancel = new Button(buttons, SWT.NONE);
		btnCancel.setImage(ImageProvider.getCommonImage("cancel.png"));
		btnCancel.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(cancelAction==null)
					close();
				else if(cancelAction.execute(null))
					close();
			}
		});
		return composite;
	}
	protected Control createDialogArea(Composite parent) {
		Composite composite = (Composite)super.createDialogArea(parent);
		GridData layoutData = new GridData();
		layoutData.horizontalAlignment = SWT.FILL;
		layoutData.verticalAlignment = SWT.FILL;
		layoutData.grabExcessVerticalSpace=true;
		layoutData.grabExcessHorizontalSpace = true;
		text = new Text(composite, SWT.MULTI);
		text.setLayoutData(layoutData);
		text.setBackground(super.getBackground());
		text.setTextLimit(255);
		return composite;
	}
	public EditablePopupDialog(Shell parent, int shellStyle, boolean takeFocusOnOpen,
			boolean persistSize, boolean persistLocation,
			boolean showDialogMenu, boolean showPersistActions,
			String titleText, String infoText) {
		super(parent, shellStyle, takeFocusOnOpen, persistSize, persistLocation,
				showDialogMenu, showPersistActions, titleText, infoText);
	}

}
